package com.hsytech.dijiebao.base;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.deparse.dpframe.common.DPConsts;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

/**
 * Created by Mo on 2015/7/6.
 */
public class BaseActivity extends FragmentActivity {

    public SystemManager Manager = SystemManager.getInstance();
    private final int ACTIVITY_NOT_FOR_RESULT = 0xFFFF;

    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Manager.addActivity(this);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // SystemManager.finishCurrActivity(this);
            Manager.finishCurrActivity(this);

        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int id) {
        return (T) findViewById(id);

    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(View view, int id) {

        return (T) view.findViewById(id);

    }

    public void callNewActivity(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(this, clazz);
        if (requestCode == ACTIVITY_NOT_FOR_RESULT) {
            startActivity(intent);
        } else {
            startActivityForResult(intent, requestCode);
        }
    }

    public void callNewActivity(Class<?> clazz) {
        callNewActivity(clazz, ACTIVITY_NOT_FOR_RESULT);
    }

    public void callNewActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg == null ? "" : msg, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int id) {
        Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
    }

    private static final String TAG = "BaseActivity";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        FragmentManager fm = getSupportFragmentManager();
        int index = requestCode >> 16;
        if (index != 0) {
            index--;
            if (fm.getFragments() == null || index < 0 || index >= fm.getFragments().size()) {
                Log.w(TAG, "Activity result fragment index out of range: 0x" + Integer.toHexString(requestCode));
                return;
            }
            Fragment frag = fm.getFragments().get(index);
            if (frag == null) {
                Log.w(TAG, "Activity result no fragment exists for index: 0x" + Integer.toHexString(requestCode));
            } else {
                handleResult(frag, requestCode, resultCode, data);
            }
            return;
        }

    }

    /**
     * �ݹ���ã���������Fragement��Ч
     *
     * @param frag
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private void handleResult(Fragment frag, int requestCode, int resultCode, Intent data) {
        frag.onActivityResult(requestCode & 0xffff, resultCode, data);
        List<Fragment> frags = frag.getChildFragmentManager().getFragments();
        if (frags != null) {
            for (Fragment f : frags) {
                if (f != null)
                    handleResult(f, requestCode, resultCode, data);
            }
        }
    }

    private class DPFinishActivityEvent {

    }

    @Subscriber(tag = "onEventMainThread")
    public void onEventMainThread(DPFinishActivityEvent event) {
        finish();
    }

    protected void finishAllActivities() {
        EventBus.getDefault().post(new DPFinishActivityEvent(), "onEventMainThread");
    }

    public void startActivityWithTaskCleared(Class<?> cls, String... extras) {
        finishAllActivities();
        Intent intent = new Intent(this, cls);
        intent.putExtra(DPConsts.ACTIVITY_START_STRING_EXTRAS, extras);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        super.startActivity(intent);
    }
}
